﻿
@{
    ViewBag.Title = "App";
    Layout = "~/Views/_LayoutApp.cshtml";
}

<script type="text/javascript">
    $(document).ready(function () {
        var ioOrderNO = '';
        var canCalc = false;

        function GetCostList() {
            $("#costList table tr:not(:first)").empty();

            var roomId = $('#RoomId').val();
            $.getJSON("/House/AppGetCostList?roomId=" + roomId, function (data) {
                ioOrderNO=data["RoomIoOrderNO"];
               canCalc = data["CanCalc"];

                $.each(data["CostList"], function (i, item) {
                    var trHTML = "<tr><td>" + ' <input id="item_Id" name="item.Id" type="hidden" value=' + item["Id"] + ' /> ' + item["Name"] + "</td><td>" + item["UnitName"] + "</td><td>" + item["Price"] + "</td><td>" + item["OldValue"] + "</td><td>" +
                        '<input type="number" name="NewValue" id="NewValue" step="0.01" min=0 value=1 style="width:60px" /></td></tr>';
                    $("#costList table").append(trHTML);
               });
                calcSumValue();
                if (canCalc == false) {
                    alert("当前房间有一张未结算单据,请先行处理付款。");
                    return;
                }
            });
        }

        function GetRoomList() {
            var houseId = $('#CategoryHouseId').val();
            if (isNaN(houseId) == false) {
                $.get("/House/AppGetRoomList/" + houseId,
                    function (data) {
                        $("#RoomId").empty();
                        var vr = (data + "").split(";");
                        $.each(vr, function (i, item) {
                            if (item.indexOf(":") >= 0)
                            {
                                var ok = item.split(":");
                                $("#RoomId").append("<option value="+ok[0]+">"+ok[1]+"</option>");
                            }
                        }); 

                        GetCostList();
                    });
            }
        }

        function calcSumValue() {
            //合计
            var sumAll = 0;
            $("tr").each(function () {
                if (!isNaN($(this).children().children().val())) {
                    var sumValue = $(this).children("td:eq(2)").text() *
                        ($(this).children("td:eq(4)").children().val() -
                           $(this).children("td:eq(3)").text());

                    if (!isNaN(sumValue)) {
                        sumAll += Number(sumValue);
                    }
                }
            });
            $("#Sum").text(sumAll.toFixed(2));
        }

        function CalcPostString() {
            //Id:Price:OldValue:NewValue
            var ps = '';
            $("tr").each(function () {
                if (!isNaN($(this).children().children().val())) {
                    ps += ($.trim($(this).children().children().val()) + ":");

                    var sumValue = $(this).children("td:eq(2)").text() *
                        ($(this).children("td:eq(4)").children().val() -
                            $(this).children("td:eq(3)").text());

                    ps += (sumValue+ ":");
                    ps += ($.trim($(this).children("td:eq(3)").text()) + ":");
                    ps += ($.trim($(this).children("td:eq(4)").children().val()) + ";");
                }
            });
            return ps;
        }

        GetRoomList();

        $('#CategoryHouseId').change(function () {
            GetRoomList();
        });

        $('#RoomId').change(function () {
            GetCostList();
        });

        $("body").on("change","input",function () { 
            calcSumValue();
        });

        $(":button").click(function () {
            if (canCalc==false) {
                alert("当前房间有一张未结算单据,请先行处理付款。");
                return;
            }

            var money = Number($("#Sum").text());
            if (money <= 0) {
                alert('金额为 0，不能进行结算。');
                return;
            }
           
            $.post("/House/CostCalc",
                {
                    roomIoOrderNO: ioOrderNO,
                    costString: CalcPostString()
                },
                function (data, status) {
                    if (status == "success") {
                        alert("提交成功"); 
                        var v = $("#RoomId").val();
                        $('#RoomId option[value="' + v + '"]').remove();
                        canCalc = false;
                    }
                    else {
                        alert("提交失败");
                    }
                });
     
        });
        });
</script>

<div class="form-horizontal">
    <div class="form-group">
        <label class="control-label col-md-2" for="CategoryHouseId">公寓</label>
        <div class="col-md-10">
            @Html.DropDownList("CategoryHouseId", ViewData["CategoryHouse"] as SelectList, htmlAttributes: new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="RoomId">房间</label>
        <div class="col-md-10">
            <select class="form-control" id="RoomId" name="RoomId"></select>
        </div>
    </div>

    <div class="form-group" id="costList"> 
        <table class="table">
            <tr>
                <th>
                    名称
                </th>
                <th>
                    单位
                </th>
                <th>
                    价格
                </th>
                <th>起始值</th>
                <th>抄表</th>
                   </tr>
            </table>
</div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="button" value="提交" class="btn btn-default" />&nbsp;&nbsp;<h3>合计：<span style="color:red;" id="Sum"></span></h3>
        </div>
    </div>

    </div>

